<?php

namespace App\Http\Controllers\Backstage\Ads;

use App\Models\Ads;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Http\Request;

//广告管理模块
class AdsController extends Controller
{
    //获取广告模块
    public function getads(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = Ads::leftjoin('upload_file', 'ads.fileid', '=', 'upload_file.fileid')
            ->orderByDesc("ads_create_time")
            ->get();
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['file_name'] = http_type() . $_SERVER['HTTP_HOST'] . '/' . $value['url'] . '/'  . $value['file_name'];
                $info[$key]['ads_create_time'] = date('Y-m-d', $value['ads_create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //获取广告详情
    public function getadsdetail(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['ads_id'] = (int) $request->query('ads_id');
        $info = Ads::where($map)
            ->leftjoin('upload_file', 'ads.fileid', '=', 'upload_file.fileid')
            ->first();
        if ($info) {
            // $info->file_name  http_type() . $_SERVER['HTTP_HOST'] . '/' . $info->url . '/'  . $info->file_name;
            $newInfo['ads_id'] = $info->ads_id;
            $newInfo['ads_link'] = $info->ads_link;
            $newInfo['ads_title'] = $info->ads_title;
            $newInfo['file_name'] = http_type() . $_SERVER['HTTP_HOST'] . '/' . $info->url . '/'  . $info->file_name;
            $data = success();
            $data['data'] = $newInfo;
        } else {
            $data = fail();
        }
        return $data;
    }

    //新增广告
    public function addads(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['ads_title'] = addslashes($request->input('ads_title'));
        $date['ads_link'] = addslashes($request->input('ads_link'));
        $date['fileid'] = (int)$request->input('fileid');
        $date['hits'] = 0;
        $date['ads_create_time'] = time();
        $msg = Ads::insert($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
    //修改广告
    public function editads(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['ads_id'] = (int)$request->input('ads_id');
        $date['ads_title'] = addslashes($request->input('ads_title'));
        $date['ads_link'] = addslashes($request->input('ads_link'));
        $date['fileid'] = (int)$request->input('fileid');
        $msg = Ads::where($map)->update($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
    //删除广告
    public function delads(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['ads_id'] = (int)$request->input('ads_id');
        $msg = Ads::where($map)->delete();
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
